/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ptolemaiosz;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Mattias
 */
public class Controller {
    
    //Referencia az application objektumra, hogy tugyjunk vele kommunikalni
    static Application application = null;  
    static Initializer initializer = null;  

//STDIN lekezelesere szolgalo fv.
static void readLine()
    {
        /////////////////////////
        //   Sor beolvasasa
        ////////////////////////
        

        try
            {   
                   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                   String input = "";
                   
                   while(input != null)
                    {
                        input = in.readLine(); 
                        if(input != null)
                        {
                            //System.err.println("\r\nCLI found: " + input + "\r\n");
                            commandParser(input); 
                        }

                     }
                
            }
       catch(Exception ex)
            {
                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
            }

    }

//STDOUT ezen megy kjeresztul
static void println(String s)
    {
        System.out.println(s);
    }

//STDOUT ezen megy kjeresztul
static void print(String s)
    {
        System.out.print(s);
    }

//Prototipu sbelepesi pont
public static void main(String[] args)
    {  
            //Application letrehozasa
            application = new Application();
            
            //Initializer letrehozasa aki ismeri az applicationt
            initializer = new Initializer();
            initializer.setApplication(application);
            
            //Uj levelpartmanager, s megtanitas az applicationnak
            application.setLevelPartManager(new LevelPartManager());
            //Uj Initilaizer, s megtanitas az applicationnak
            application.setInitializer(initializer);
            //Uj Physics, s megtanitas az applicationnak
            application.setPhysics(new Physics());   
            
            readLine();
            
    }
    
public static void commandParser(String input)
    {
                                            /////////////////////////
                                            //   Matcher inicializalasa
                                            ////////////////////////
                                            Matcher m;
                                            boolean foundMatch = false;

                                            /////////////////////////
                                            //   Regexp Mintak
                                            ////////////////////////
                                            Pattern loadLevel = Pattern.compile("^loadLevel ([0-9]+)$");
                                            Pattern update    = Pattern.compile("^update\\(([0-9]+).00\\)$");
                                            Pattern update2    = Pattern.compile("^update ([0-9]+)$");
                                            Pattern changeViewmode = Pattern.compile("^ $");
                                            Pattern changeViewmode2 = Pattern.compile("^'[ ]*'$");
                                            Pattern keyW = Pattern.compile("^w$");
                                            Pattern keyA = Pattern.compile("^a$");
                                            Pattern keyS = Pattern.compile("^s$");
                                            Pattern keyD = Pattern.compile("^d$");
                                            Pattern keyI = Pattern.compile("^i$");
                                            Pattern keyJ = Pattern.compile("^j$");
                                            Pattern keyL = Pattern.compile("^l$");
                                            Pattern keyN = Pattern.compile("^n$");
                                            Pattern keyR = Pattern.compile("^r$");


                                            /////////////////////////
                                            //   Mintaillesztesek
                                            ////////////////////////

                                            //Loadlevel int    
                                                m = loadLevel.matcher(input);

                                                if(m.find())
                                                    {
                                                        foundMatch = true;
                                                        initializer.loadLevel(Integer.parseInt(m.group(1)));
                                                    }

                                            //Update int    
                                                m = update.matcher(input);

                                                if(m.find())
                                                    {
                                                        foundMatch = true;
                                                        application.getPhysics().update(Integer.parseInt(m.group(1)));
                                                    }

                                                m = update2.matcher(input);

                                                if(m.find())
                                                    {
                                                        foundMatch = true;
                                                        application.getPhysics().update(Integer.parseInt(m.group(1)));
                                                    }

                                            //Change ViewMode
                                                m = changeViewmode.matcher(input);

                                                if(m.find())
                                                    {
                                                        foundMatch = true;
                                                        application.chageViewMode();
                                                    }

                                                m = changeViewmode2.matcher(input);

                                                if(m.find())
                                                    {
                                                        foundMatch = true;
                                                        application.chageViewMode();
                                                    }

                                                //KEY W
                                                m = keyW.matcher(input);

                                                if (m.find()) {
                                                    foundMatch = true;

                                                    //Majd o ellenorzi mozaik nezetben van-e
                                                    application.moveLevelPart(Direction.up);

                                                    //Majd o ellenorzi jatekos nezetben van-e
                                                    application.moveAvatar(Direction.up, 1);

                                                }

                                                //KEY A
                                                m = keyA.matcher(input);

                                                if (m.find()) {
                                                    foundMatch = true;

                                                    //Majd o ellenorzi mozaik nezetben van-e
                                                    application.moveLevelPart(Direction.left);

                                                    //Majd o ellenorzi jatekos nezetben van-e
                                                    application.moveAvatar(Direction.left, 1);

                                                }

                                                //KEY S
                                                m = keyS.matcher(input);

                                                if (m.find()) {
                                                    foundMatch = true;

                                                    //Majd o ellenorzi mozaik nezetben van-e
                                                    application.moveLevelPart(Direction.down);
                                                }

                                                //KEY D
                                                m = keyD.matcher(input);

                                                if(m.find())
                                                    {
                                                        foundMatch = true;

                                                        //Majd o ellenorzi mozaik nezetben van-e
                                                        //application.moveLevelPart(Direction.right);
                                                        application.moveAvatar(Direction.right, 1);

                                                        //Majd o ellenorzi mozaik nezetben van-e
                                                    application.moveLevelPart(Direction.right);

                                                    }

                                                //KEY I
                                                m = keyI.matcher(input);

                                                if (m.find()) {
                                                    foundMatch = true;
                                                    //Majd o ellenorzi jatekos nezetben van-e
                                                    application.moveAvatar(Direction.up, 2);
                                                }

                                                //KEY J
                                                m = keyJ.matcher(input);

                                                if (m.find()) {
                                                    foundMatch = true;
                                                    //Majd o ellenorzi jatekos nezetben van-e
                                                    application.moveAvatar(Direction.left, 2);
                                                }

                                                //KEY L
                                                m = keyL.matcher(input);

                                                if (m.find()) {
                                                    foundMatch = true;
                                                    //Majd o ellenorzi jatekos nezetben van-e
                                                    application.moveAvatar(Direction.right, 2);
                                                }

                                                //KEY N
                                                m = keyN.matcher(input);

                                                if (m.find()) {
                                                    foundMatch = true;
                                                    //Majd o ellenorzi jatekos nezetben van-e
                                                    application.nextLevel();
                                                }

                                                //KEY R
                                                m = keyR.matcher(input);

                                                if (m.find()) {
                                                    foundMatch = true;
                                                    //Majd o ellenorzi jatekos nezetben van-e
                                                    application.restartLevel();
                                                }




                                            //Hibas parancs
                                            if(!foundMatch)
                                                println("HIBA: ilyen parancs nem letezik, vagy hianyzik/hibas a parameter");
    }


}